// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.editors;

import java.util.ArrayList;
import java.util.List;

import org.dengues.designer.ui.database.editors.GEFDatabaseEditor;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.process.editors.GEFComponentsEditor;
import org.eclipse.gef.editparts.ZoomManager;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.actions.DeleteRetargetAction;
import org.eclipse.gef.ui.actions.GEFActionConstants;
import org.eclipse.gef.ui.actions.RedoRetargetAction;
import org.eclipse.gef.ui.actions.UndoRetargetAction;
import org.eclipse.gef.ui.actions.ZoomComboContributionItem;
import org.eclipse.gef.ui.actions.ZoomInRetargetAction;
import org.eclipse.gef.ui.actions.ZoomOutRetargetAction;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.RetargetAction;
import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class MultiPageEditorContributor extends MultiPageEditorActionBarContributor {

    private IEditorPart activeEditorPart;

    private ActionRegistry registry = new ActionRegistry();

    private List<RetargetAction> retargetActions = new ArrayList<RetargetAction>();

    /**
     * Qiang.Zhang.Adolf@gmail.com MultiPageEditorContributor constructor comment.
     */
    public MultiPageEditorContributor() {
        super();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorActionBarContributor#init(org.eclipse.ui.IActionBars)
     */
    @Override
    public void init(IActionBars bars) {
        addDesignRetargetAction(new ZoomInRetargetAction());
        addDesignRetargetAction(new ZoomOutRetargetAction());
        addDesignRetargetAction(new DeleteRetargetAction());
        addDesignRetargetAction(new UndoRetargetAction());
        addDesignRetargetAction(new RedoRetargetAction());

        super.init(bars);
    }

    protected void addDesignRetargetAction(final RetargetAction action) {
        registry.registerAction(action);
        retargetActions.add(action);
        getPage().addPartListener(action);
    }

    /**
     * Returns the action registed with the given text editor.
     * 
     * @return IAction or null if editor is null.
     */
    protected IAction getAction(IEditorPart editor, String actionID) {
        if (editor instanceof ITextEditor) {
            return ((ITextEditor) editor).getAction(actionID);
        }
        if (editor instanceof GEFComponentsEditor) {
            return ((GEFComponentsEditor) editor).getAction(actionID);
        }
        if (editor instanceof GEFDatabaseEditor) {
            return ((GEFDatabaseEditor) editor).getAction(actionID);
        }

        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.MultiPageEditorActionBarContributor#setActivePage(org.eclipse.ui.IEditorPart)
     */
    @Override
    public void setActivePage(IEditorPart activeEditor) {
        if ((activeEditorPart == activeEditor) || (activeEditor == null)) {
            return;
        }

        activeEditorPart = activeEditor;

        IActionBars actionBars = getActionBars();
        if (actionBars != null) {

            setGlobalActionHandlers(activeEditor, actionBars);
            actionBars.updateActionBars();
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "setsetGlobalActionHandlers".
     * 
     * @param activeEditor
     * @param actionBars
     */
    protected void setGlobalActionHandlers(IEditorPart activeEditor, IActionBars actionBars) {
        actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), getAction(activeEditor, ActionFactory.DELETE.getId()));
        actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), getAction(activeEditor, ActionFactory.UNDO.getId()));
        actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), getAction(activeEditor, ActionFactory.REDO.getId()));
        actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), getAction(activeEditor, ActionFactory.CUT.getId()));
        actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), getAction(activeEditor, ActionFactory.COPY.getId()));
        actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), getAction(activeEditor, ActionFactory.PASTE.getId()));
        actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), getAction(activeEditor, ActionFactory.SELECT_ALL
                .getId()));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorActionBarContributor#contributeToMenu(org.eclipse.jface.action.IMenuManager)
     */
    @Override
    public void contributeToMenu(IMenuManager menuManager) {
        super.contributeToMenu(menuManager);
        MenuManager viewMenu = new MenuManager(Messages.getString("MultiPageEditorContributor.menuView.Label")); //$NON-NLS-1$
        viewMenu.add(getAction(GEFActionConstants.ZOOM_IN));
        viewMenu.add(getAction(GEFActionConstants.ZOOM_OUT));
        viewMenu.add(new Separator());
        // viewMenu.add(getAction(GEFActionConstants.TOGGLE_GRID_VISIBILITY));
        // viewMenu.add(getAction(GEFActionConstants.TOGGLE_SNAP_TO_GEOMETRY));
        menuManager.insertAfter(IWorkbenchActionConstants.M_EDIT, viewMenu);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorActionBarContributor#contributeToToolBar(org.eclipse.jface.action.IToolBarManager)
     */
    @Override
    public void contributeToToolBar(IToolBarManager toolBarManager) {
        super.contributeToToolBar(toolBarManager);
        toolBarManager.add(getAction(ActionFactory.UNDO.getId()));
        toolBarManager.add(getAction(ActionFactory.REDO.getId()));
        String[] zoomStrings = new String[] { ZoomManager.FIT_ALL, ZoomManager.FIT_HEIGHT, ZoomManager.FIT_WIDTH };
        toolBarManager.add(new ZoomComboContributionItem(getPage(), zoomStrings));
    }

    protected IAction getAction(final String id) {
        return registry.getAction(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorActionBarContributor#dispose()
     */
    @Override
    public void dispose() {
        super.dispose();
        IActionBars actionBars = getActionBars();
        if (actionBars != null) {
            actionBars.clearGlobalActionHandlers();
        }
        for (RetargetAction action : retargetActions) {
            getPage().removePartListener(action);
        }
        activeEditorPart = null;
        retargetActions = null;
        registry = null;
    }
}
